﻿using ScanConfirm.Business;
using ScanConfirm.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScanConfirmForm
{
    public partial class FrmCarTypeSet : Form
    {
        CarTypeBLL _CarTypeBLL = new CarTypeBLL();
        CarTypeModel _CarTypeModel;
        public Action RefreshUiEvent;
        public FrmCarTypeSet()
        {
            InitializeComponent();
        }

        public FrmCarTypeSet(CarTypeModel carTypeModel)
        {
            InitializeComponent();
            _CarTypeModel = carTypeModel;
            txtCarCode.Text = carTypeModel.CarCode;
            txtCarName.Text = carTypeModel.CarName;
            btnSave.Text = "修改";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (_CarTypeModel == null)
            {
                var model = _CarTypeBLL.GetCarModel(txtCarCode.Text, txtCarName.Text);
                if (model != null)
                {
                    MessageBox.Show("已存在相同的车型配置，不能重复！");
                    return;
                }
                _CarTypeModel = new CarTypeModel();
                _CarTypeModel.CarName = txtCarName.Text;
                _CarTypeModel.CarCode = txtCarCode.Text;
                _CarTypeModel.SyncFlag = 0;
                var result = _CarTypeBLL.Add(_CarTypeModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("添加车型成功");
                    RefreshUiEvent?.BeginInvoke(null,null);
                    this.Close();
                }
                else
                    MessageBox.Show("添加车型失败：" + result.Dsca);
            }
            else
            {
                _CarTypeModel.CarName = txtCarName.Text;
                _CarTypeModel.CarCode = txtCarCode.Text;
                _CarTypeModel.SyncFlag = 2;
                var result = _CarTypeBLL.Update(_CarTypeModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("修改车型成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("修改车型失败：" + result.Dsca);
            }
        }
    }
}
